var mjfeed = {
	base: "",
	locale: "zh_CN"
};

var basePath = "";
$().ready(function(){
	basePath = $("#basePath").val();
	
	//顶部 是否登陆
	$headerLogin = $("#headerLogin");
	$headerRegister = $("#headerRegister");
	$headerShownameLi = $("#headerShownameLi");
	$headerShowname = $("#headerShowname");
	$headerLogout = $("#headerLogout");
	$toprightnav = $("#toprightnav");
	if($toprightnav != null) {
		var username = getCookie("username");
		if (username != null) {
			$headerShowname.text(username);
			$headerShownameLi.show();
			$headerLogout.show();
		} else {
			$headerLogin.show();
			$headerRegister.show();
		}	
	}
});

//立即购买
function buyNow(productId) {
	addCar(productId, "buyNow");
}
//添加购物车
function addCart(productId) {
	addCar(productId, "addShoppingCar");
}

function addCar(productId, flag) {
	var quantity = 1;
	$.ajax({
		type : "post",
		url : basePath+"cart/add.shtml",
		data : {"id":productId , "quantity":quantity },
		dataType : "json",
		cache: false,
		success: function(data){
			var success = data.success;
			if (success == "true") {
				if (flag != null  && flag == "addShoppingCar") {
					alert("加入成功");
				} else {
					window.location.href=basePath+"m/cart.shtml";
				}
			} else {
				alert("购买失败");
			}
		},
		error :function() {
		}
	});
}

//去购物车
function go2Car() {
	window.location.href=basePath+"m/cart.shtml";
}

//添加Cookie
function addCookie(name, value, options) {
	if (arguments.length > 1 && name != null) {
		if (options == null) {
			options = {};
		}
		if (value == null) {
			options.expires = -1;
		}
		if (typeof options.expires == "number") {
			var time = options.expires;
			var expires = options.expires = new Date();
			expires.setTime(expires.getTime() + time * 1000);
		}
		document.cookie = encodeURIComponent(String(name)) + "=" + encodeURIComponent(String(value)) + (options.expires ? "; expires=" + options.expires.toUTCString() : "") + (options.path ? "; path=" + options.path : "") + (options.domain ? "; domain=" + options.domain : ""), (options.secure ? "; secure" : "");
	}
}

// 获取Cookie
function getCookie(name) {
	if (name != null) {
		var value = new RegExp("(?:^|; )" + encodeURIComponent(String(name)) + "=([^;]*)").exec(document.cookie);
		return value ? decodeURIComponent(value[1]) : null;
	}
}

// 移除Cookie
function removeCookie(name, options) {
	addCookie(name, null, options);
}

// 百度统计
//var _hmt = _hmt || [];
//(function() {
//  var hm = document.createElement("script");
//  hm.src = "//hm.baidu.com/hm.js?9bf78ebe7d3534adb2d0c866a6ef16b4";
//  var s = document.getElementsByTagName("script")[0]; 
//  s.parentNode.insertBefore(hm, s);
//})();